﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pomodoro.Core.View;
using Pomodoro.Core.Presenter;
using Pomodoro.Core.Model.Domain;

namespace Pomodoro.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class PomodoroPresenterTest
    {

        private PomodoroPresenter presenter;
        public PomodoroPresenterTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        [TestInitialize()]
        public void MyTestInitialize() {
            presenter = new PomodoroPresenter();
            presenter.Clear();
        }

        [TestMethod]
        public void Permite_adicionar_uma_nova_tarefa()
        {
            string mensagemOperacao = new AlertsHelper().GetAlert(Alerts.TarefaAddSucesso);
            MockTaskView view = new MockTaskView
            {
                NewTask = "Testar adição de tarefa",
                ListTasks = null,
                Alerts = String.Empty
            };

            presenter = new PomodoroPresenter(view);
            presenter.AddTask(view.NewTask);

            Assert.AreEqual(mensagemOperacao, view.Alerts);
        }

        [TestMethod]
        public void Atualiza_lista_tarefas_pos_inclusao()
        {

            string mensagemOperacao = new AlertsHelper().GetAlert(Alerts.TarefaAddSucesso);
            MockTaskView view = new MockTaskView
            {
                NewTask = "Testar a lista de novas tarefas",
                Alerts = String.Empty,
                ListTasks = null
            };

            presenter = new PomodoroPresenter(view);
            presenter.AddTask(view.NewTask);

            Assert.AreEqual(mensagemOperacao, view.Alerts);
            Assert.IsNotNull(view.ListTasks);
            Assert.IsTrue(view.ListTasks.Count > 0);
        }

        [TestMethod]
        public void Alertar_adicao_nova_tarefa_vazia()
        {

            string mensagemOperacao = new AlertsHelper().GetAlert(Alerts.CampoTarefaObrigatorio);
            MockTaskView view = new MockTaskView
            {
                NewTask = String.Empty,
                Alerts = String.Empty,
                ListTasks = null
            };

            presenter = new PomodoroPresenter(view);
            presenter.AddTask(view.NewTask);

            Assert.AreEqual(mensagemOperacao, view.Alerts);
        }

        [TestMethod]
        public void Pesquisar_tarefa_do_backlog()
        {
            MockTaskView view = new MockTaskView
            {
                NewTask = "Pesquisar tarefa existente no backlog",
                Alerts = String.Empty,
                ListTasks = null
            };

            presenter = new PomodoroPresenter(view);
            presenter.AddTask(view.NewTask);

            Task fromDB = presenter.GetTaskByDescription(view.NewTask);
            Assert.IsNotNull(fromDB);

        }

        [TestMethod]
        public void Pesquisar_tarefa_nao_existente_no_backlog()
        {
            MockTaskView view = new MockTaskView
            {
                NewTask = "Pesquisar tarefa não existente no backlog",
                Alerts = String.Empty,
                ListTasks = null
            };

            presenter = new PomodoroPresenter(view);
            presenter.AddTask(view.NewTask);

            Task fromDB = presenter.GetTaskByDescription(String.Empty);
            Assert.IsNull(fromDB);

        }

        [TestMethod]
        public void Permitir_cancelar_tarefa_do_backlog()
        {
            string mensagem = new AlertsHelper().GetAlert(Alerts.TarefaCanceladaSucesso);
            MockTaskView view = new MockTaskView
            {
                NewTask = "Permitir cancelar tarefa do backlog",
                Alerts = String.Empty,
                ListTasks = null
            };

            presenter = new PomodoroPresenter(view);
            presenter.AddTask(view.NewTask);
            presenter.CancelTask(view.NewTask);

            Task fromListTasks = (from task
                                      in view.ListTasks
                                  where task.Description.Equals(view.NewTask)
                                  select task).FirstOrDefault<Task>();

            Assert.IsNull(fromListTasks);
            Assert.IsTrue(view.ListTasks.Count <= 0);
            Assert.AreEqual(mensagem, view.Alerts);
        }

        [TestMethod]
        public void Alertar_tarefa_nao_exitente_no_backlog_evento_cancelar()
        {
            string mensagem = new AlertsHelper().GetAlert(Alerts.TarefaNaoExiste);
            MockTaskView view = new MockTaskView
            {
                NewTask = "Alertar tarefa não existente (evento cancelar)",
                Alerts = String.Empty,
                ListTasks = null
            };

            presenter = new PomodoroPresenter(view);
            presenter.AddTask(view.NewTask);
            presenter.CancelTask(String.Empty);

            Assert.IsTrue(view.ListTasks.Count > 0);
            Assert.AreEqual(mensagem, view.Alerts);
        }

        [TestMethod]
        public void Permite_iniciar_uma_tarefa()
        {
            string mensagem = new AlertsHelper().GetAlert(Alerts.TarefaIniciadaSucesso);
            string novaTarefa = "Iniciar a tarefa";

            MockTaskView view = new MockTaskView
            {
                NewTask = String.Empty,
                Alerts = String.Empty,
                ListTasks = null
            };

            presenter = new PomodoroPresenter(view);
            presenter.AddTask(novaTarefa);


            int taskID = (from t
                              in view.ListTasks
                          where t.Description.Equals(novaTarefa)
                          select t.ID).FirstOrDefault<int>();

            presenter.StartTask(taskID);

            Task fromListView = (from t
                                    in view.ListTasks
                                 where t.ID == taskID
                                 select t).FirstOrDefault<Task>();

            Assert.IsNotNull(fromListView);
            Assert.IsTrue(fromListView.Started);
            Assert.AreEqual(mensagem, view.Alerts);
        }


        [TestMethod]
        public void Alerta_tarefa_ja_iniciada()
        {
            string mensagem = new AlertsHelper().GetAlert(Alerts.ExisteTarefaIniciada);
            string novaTarefa = "iniciar esta tarefa";
            string novaTarefa2 = "proibir inicio desta tarefa";

            MockTaskView view = new MockTaskView
            {
                NewTask = String.Empty,
                Alerts = String.Empty,
                ListTasks = null
            };

            presenter = new PomodoroPresenter(view);
            presenter.AddTask(novaTarefa);
            presenter.AddTask(novaTarefa2);


            int taskID = (from t
                              in view.ListTasks
                          where t.Description.Equals(novaTarefa)
                          select t.ID).FirstOrDefault<int>();
            presenter.StartTask(taskID);

            int taskID2 = (from t
                              in view.ListTasks
                           where t.Description.Equals(novaTarefa2)
                           select t.ID).FirstOrDefault<int>();
            presenter.StartTask(taskID2);

            Task fromListView = (from t
                                    in view.ListTasks
                                 where t.ID == taskID2
                                 select t).FirstOrDefault<Task>();

            Assert.IsNotNull(fromListView);
            Assert.IsFalse(fromListView.Started);
            Assert.AreEqual(mensagem, view.Alerts);
        }

        [TestMethod]
        public void Checar_conclusao_tarefa()
        {
            string mensagem = new AlertsHelper().GetAlert(Alerts.TarefaConcluida);
            string novaTarefa = "Checar conslusão da tarefa";

            MockTaskView view = new MockTaskView
            {
                NewTask = String.Empty,
                Alerts = String.Empty,
                ListTasks = null
            };

            presenter = new PomodoroPresenter(view);
            presenter.AddTask(novaTarefa);

            int taskID = (from t
                              in view.ListTasks
                          where t.Description.Equals(novaTarefa)
                          select t.ID).FirstOrDefault<int>();
            presenter.StartTask(taskID);
            Task task = presenter.GetTaskByDescription(novaTarefa);
            TimeSpan ts = new TimeSpan(0, 25, 0);
            DateTime timeTest = task.TimeStarted.Add(ts);

            presenter.CheckTime(timeTest.Ticks);
            task = presenter.GetTaskByDescription(novaTarefa);

            Assert.IsTrue(task.Status);
            Assert.AreEqual(mensagem, view.Alerts);
        }
    }

    public class MockTaskView : TaskView
    {

        #region TaskView Members

        public string NewTask { get; set; }
        public string CurrentTask { get; set; }
        public string Alerts { get; set; }
        public ICollection<Pomodoro.Core.Model.Domain.Task> ListTasks { get; set; }

        #endregion
    }
}
